
<!-- <template>
  <div>
    <button @click="exportToExcel">Export to Excel</button>
    <input type="file" @change="handleFileUpload">
  </div>
</template>

<script>
import * as XLSX from 'xlsx';

export default {
  methods: {
    exportToExcel() {
      // 准备要导出的数据
      const data = [
        { name: 'John', age: 30, city: 'New York' },
        { name: 'Mike', age: 25, city: 'Chicago' },
        { name: 'Sara', age: 28, city: 'Los Angeles' }
      ];

      // 将数据转换为工作表
      const ws = XLSX.utils.json_to_sheet(data);
      // 创建工作簿并添加工作表
      const wb = XLSX.utils.book_new();
      XLSX.utils.book_append_sheet(wb, ws, "Sheet1");

      // 导出Excel文件
      XLSX.writeFile(wb, "data.xlsx");
    },
    handleFileUpload(event) {
      const file = event.target.files[0];
      const reader = new FileReader();
      reader.onload = (e) => {
        const data = e.target.result;
        const workbook = XLSX.read(data, { type: 'binary' });
        const sheetName = workbook.SheetNames[0];
        const worksheet = workbook.Sheets[sheetName];
        const json = XLSX.utils.sheet_to_json(worksheet);
        console.log(json); // 处理或使用数据
      };
      reader.readAsBinaryString(file);
    }
  }
};
</script> -->



<!-- <template>
  <div>
    <button @click="exportToExcel">Export to Excel</button>
    <input type="file" @change="handleFileUpload">
  </div>
</template>

<script setup>
import { ref } from 'vue';
import * as XLSX from 'xlsx';

// 准备要导出的数据
const data = ref([
  { name: 'John', age: 30, city: 'New York' },
  { name: 'Mike', age: 25, city: 'Chicago' },
  { name: 'Sara', age: 28, city: 'Los Angeles' }
]);

const exportToExcel = () => {
  // 将数据转换为工作表
  const ws = XLSX.utils.json_to_sheet(data.value);
  // 创建工作簿并添加工作表
  const wb = XLSX.utils.book_new();
  XLSX.utils.book_append_sheet(wb, ws, "Sheet1");

  // 导出Excel文件
  XLSX.writeFile(wb, "data.xlsx");
};

const handleFileUpload = (event) => {
  const file = event.target.files[0];
  const reader = new FileReader();
  reader.onload = (e) => {
    const data = e.target.result;
    const workbook = XLSX.read(data, { type: 'binary' });
    const sheetName = workbook.SheetNames[0];
    const worksheet = workbook.Sheets[sheetName];
    const json = XLSX.utils.sheet_to_json(worksheet);
    console.log(json); // 处理或使用数据
  };
  reader.readAsBinaryString(file);
};
</script> -->



<template>
  <div>
    <button @click="exportData">Export to Excel</button>
    <input type="file" @change="importData" />
    <button @click="exportDataAsZip">Export Data to Zip</button>
    <button @click="exportPhotoAsZip">Export Photo to Zip</button>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useExcel } from './utils/useExcel';
import { useZip } from './utils/useZip';

const { exportToExcel, importExcel } = useExcel();
const { createZipAndDownload, zipFiles } = useZip();

const exportData = async () => {
  const data = [
    { name: 'John', age: 30, city: 'New York' },
    { name: 'Mike', age: 25, city: 'Chicago' },
    { name: 'Sara', age: 28, city: 'Los Angeles' }
  ];
  await exportToExcel(data);
};

const importData = async (event) => {
  const file = event.target.files[0];
  try {
    const data = await importExcel(file);
    console.log(data); // 处理或使用数据
  } catch (error) {
    console.error('Error importing Excel file:', error);
  }
};

const exportDataAsZip = () => {
  const data = [
    { name: 'John', age: 30, city: 'New York' },
    { name: 'Mike', age: 25, city: 'Chicago' },
    { name: 'Sara', age: 28, city: 'Los Angeles' }
  ];
  createZipAndDownload(data);
};


const exportPhotoAsZip = async () => {
  // 图片的链接
  const photoUrl = 'https://statics.moonshot.cn/kimi-chat/favicon.ico'; // 替换为实际的图片链接
  try {
    // 使用fetch API从链接下载图片
    const response = await fetch(photoUrl);
    const blob = await response.blob(); // 将响应体转换为Blob对象
    // 创建文件数组，包含下载的图片文件
    const files = [
      {
        name: 'favicon.ico', // 假设图片文件名为photo.jpg
        content: blob // 直接使用Blob对象
      }
    ];

    // 调用封装的方法，创建zip并下载
    zipFiles(files, 'data.zip');
  } catch (error) {
    console.error('Error downloading or creating zip:', error);
  }
};


</script>